package com.haidechizi.i18n.spring.boot.autoconfigure;


import com.haidechizi.i18n.common.cache.CacheService;
import com.haidechizi.i18n.common.cache.RedisCacheService;
import com.haidechizi.i18n.common.cache.persistent.RedisService;
import com.haidechizi.i18n.common.cache.persistent.RedisUtils;
import com.haidechizi.i18n.spring.boot.properties.I18nCacheProperties;
import com.haidechizi.i18n.spring.boot.properties.I18nProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;


/**
 * 国际化数据缓存配置类
 */
@Configuration
@EnableConfigurationProperties(I18nCacheProperties.class)
@ConditionalOnProperty(prefix = I18nProperties.I18N_PREFIX, name = "i18n-use-cache", havingValue = "true")
public class I18nCacheAutoConfiguration implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private I18nCacheProperties i18nCacheProperties;

    @Bean
    @ConditionalOnMissingBean
    public CacheService cacheService() {
        return new RedisCacheService();
    }

    @Bean
    @ConditionalOnMissingBean
    public RedisService redisService() {
        RedisService redisService = new RedisUtils();
        ((RedisUtils) redisService).setJedisPool(jedisPool());
        return redisService;
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = I18nCacheProperties.I18N_CACHE_PREFIX, name = "jedis-ip")
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(i18nCacheProperties.getJedisMaxTotal());
        jedisPoolConfig.setMaxIdle(i18nCacheProperties.getJedisMaxIdle());
        jedisPoolConfig.setMaxWaitMillis(i18nCacheProperties.getJedisMaxWaitMillis());
        jedisPoolConfig.setTestOnBorrow(i18nCacheProperties.isJedisTestOnBorrow());
        return jedisPoolConfig;
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = I18nCacheProperties.I18N_CACHE_PREFIX, name = "jedis-ip")
    @ConditionalOnBean(JedisPoolConfig.class)
    public JedisPool jedisPool() {
        String password = i18nCacheProperties.getJedisPassword();
        if (StringUtils.isBlank(password)) {
            password = null;
        }
        return new JedisPool(jedisPoolConfig(), i18nCacheProperties.getJedisIp(), i18nCacheProperties.getJedisPort(),
                i18nCacheProperties.getJedisTimeout(), password);
    }


    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

    }
}
